package com.huawei.esdk.uc.u19.north.rest.app.resource.ctc.convert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.DateUtils;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.uc.common.CipherUtilsRest;
import com.huawei.esdk.uc.common.ErrInfo;
import com.huawei.esdk.uc.domain.model.Conference;
import com.huawei.esdk.uc.u19.north.rest.bean.ctc.ConferenceInfo;
import com.huawei.esdk.uc.u19.north.rest.bean.ctc.EndConfResponse;
import com.huawei.esdk.uc.u19.north.rest.bean.ctc.QueryConfResponse;
import com.huawei.esdk.uc.u19.north.rest.bean.ctc.QueryConferenceInfo;

public class CTCResourceConvert
{
    private static final Logger LOGGER = Logger.getLogger(CTCResourceConvert.class);
    
    public Conference getConferenceInfoRest2Model(ConferenceInfo confInfo)
        throws SDKException
    {
        final String TRUEFLAG = "1";
        
        Conference conf = new Conference();
        Date startTime = DateUtils.stringToDate(confInfo.getStartTime(), "yyyy-MM-dd HH:mm:ss");
        Date endTime = DateUtils.stringToDate(confInfo.getEndTime(), "yyyy-MM-dd HH:mm:ss");
        
        //验证startTime，endTime格式是否正确
        if (null != startTime && null != endTime)
        {
            conf.setStartTime(startTime);
            conf.setEndTime(endTime);
        }
        else
        {
            LOGGER.info("startTime or endTime is not correct");
            SDKException sdkException = new SDKException(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            sdkException.setSdkErrCode(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE);
            throw sdkException;
        }
        
        //confRoom必须为Integer
        if (StringUtils.isInteger(confInfo.getConfRoom()))
        {
            conf.setConfRoom(Integer.parseInt(confInfo.getConfRoom()));
        }
        else
        {
            LOGGER.info("ConfRoom should be a number");
            SDKException sdkException = new SDKException(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            sdkException.setSdkErrCode(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE);
            throw sdkException;
        }
        
        //syncConfType必须为Integer
        if (StringUtils.isInteger(confInfo.getSyncConfType()))
        {
            conf.setConfMode(confInfo.getSyncConfType());
        }
        else
        {
            LOGGER.info("SyncConfType should be a number");
            SDKException sdkException = new SDKException(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            sdkException.setSdkErrCode(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE);
            throw sdkException;
        }
        
        //srtpUsageMode必须为Integer
        if (StringUtils.isInteger(confInfo.getSrtpUsageMode()))
        {
            conf.setSrtpUsageMode(confInfo.getSrtpUsageMode());
        }
        else
        {
            LOGGER.info("srtpUsageMode should be a number");
            SDKException sdkException = new SDKException(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            sdkException.setSdkErrCode(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE);
            throw sdkException;
        }
        
        //对attendPwd解密
        String attendPwd = CipherUtilsRest.decodeFromBase64(confInfo.getAttendPwd());
        if (StringUtils.isEmpty(attendPwd))
        {
            SDKException sdkException = new SDKException(ErrInfo.SDK_UC_PASSWORD_DECODE_ERRORDESC);
            sdkException.setSdkErrCode(ErrInfo.SDK_UC_PASSWORD_DECODE_ERRORCODE);
            throw sdkException;
        }
        conf.setConfMemberPwd(attendPwd);
        
        //对chairmanPwd解密
        String chairmanPwd = CipherUtilsRest.decodeFromBase64(confInfo.getChairmanPwd());
        if (StringUtils.isEmpty(chairmanPwd))
        {
            SDKException sdkException = new SDKException(ErrInfo.SDK_UC_PASSWORD_DECODE_ERRORDESC);
            sdkException.setSdkErrCode(ErrInfo.SDK_UC_PASSWORD_DECODE_ERRORCODE);
            throw sdkException;
        }
        conf.setConfChairPwd(chairmanPwd);
        
        // 入会提示音类型和离会提示音类型必须为0或1
        if ((!"0".equals(confInfo.getEnterPrompt()) && !"1".equals(confInfo.getEnterPrompt()))
            || !"0".equals(confInfo.getLeavePrompt()) && !"1".equals(confInfo.getLeavePrompt()))
        {
            SDKException sdkException = new SDKException(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            sdkException.setSdkErrCode(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE);
            throw sdkException;
        }
        
        // 校验gwIp参数的格式
        if (!StringUtils.isIp(confInfo.getGwIp()))
        {
            SDKException sdkException = new SDKException(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            sdkException.setSdkErrCode(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE);
            throw sdkException;
        }
        
        conf.setSubject(confInfo.getConfName());
        conf.setSubject(confInfo.getConfName());
        conf.setEnterPrompt(confInfo.getEnterPrompt());
        conf.setGwIp(confInfo.getGwIp());
        conf.setIfRecord(TRUEFLAG.equals(confInfo.getIsConfRecord()));
        conf.setIsSuperConf(TRUEFLAG.equals(confInfo.getIsSuperConf()));
        conf.setLeavePrompt(confInfo.getLeavePrompt());
        conf.setMostNo(Integer.parseInt(confInfo.getRegSeatCount()));
        conf.setMaxVoice(confInfo.getSpeakerCount());
        conf.setSubPbx(confInfo.getSubPbxNo());
        conf.setInitiatorId(confInfo.getUserId());
        return conf;
    }
    
    public QueryConfResponse getQueryConfResponseModel2Rest(SDKResult<List<Conference>> result)
        throws SDKException
    {
        QueryConfResponse response = new QueryConfResponse();
        response.setResultCode(String.valueOf(result.getErrCode()));
        response.setResultDesc(StringUtils.avoidNull(result.getDescription()));
        
        if (0 == result.getErrCode() && null != result.getResult())
        {
            List<QueryConferenceInfo> confs = new ArrayList<QueryConferenceInfo>();
            for (Conference confModal : result.getResult())
            {
                QueryConferenceInfo confRest = getConferenceInfoModel2Rest(confModal);
                if (null != confRest)
                {
                    confs.add(confRest);
                }
            }
            response.setConfList(confs);
            response.setConferenceCount(confs.size());
        }
        
        return response;
    }
    
    private QueryConferenceInfo getConferenceInfoModel2Rest(Conference confModel)
        throws SDKException
    {
        if (null == confModel)
        {
            return null;
        }
        
        QueryConferenceInfo confRest = new QueryConferenceInfo();
        
        //对ConfMemberPwd加密
        if (!StringUtils.isEmpty(confModel.getConfMemberPwd()))
        {
            String password = null;
            try
            {
                password = CipherUtilsRest.encode(confModel.getConfMemberPwd());
            }
            catch (SDKException e)
            {
                throw e;
            }
            if (StringUtils.isEmpty(password))
            {
                SDKException sdkException = new SDKException(ErrInfo.SDK_UC_PASSWORD_ENCODE_ERRORDESC);
                sdkException.setSdkErrCode(ErrInfo.SDK_UC_PASSWORD_ENCODE_ERRORCODE);
                throw sdkException;
            }
            confRest.setAttendPwd(password);
        }
        else
        {
            confRest.setAttendPwd(confModel.getConfMemberPwd());
        }
        
        //对ConfChairPwd加密
        if (!StringUtils.isEmpty(confModel.getConfChairPwd()))
        {
            String password = null;
            try
            {
                password = CipherUtilsRest.encode(confModel.getConfChairPwd());
            }
            catch (SDKException e)
            {
                throw e;
            }
            if (StringUtils.isEmpty(password))
            {
                SDKException sdkException = new SDKException(ErrInfo.SDK_UC_PASSWORD_ENCODE_ERRORDESC);
                sdkException.setSdkErrCode(ErrInfo.SDK_UC_PASSWORD_ENCODE_ERRORCODE);
                throw sdkException;
            }
            confRest.setChairmanPwd(password);
        }
        else
        {
            confRest.setChairmanPwd(confModel.getConfChairPwd());
        }
        
        confRest.setStartTime(DateUtils.dateToString(confModel.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
        confRest.setEndTime(DateUtils.dateToString(confModel.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
        confRest.setConfRoom(String.valueOf(confModel.getConfRoom()));
        confRest.setEnterPrompt(confModel.getEnterPrompt());
        confRest.setIsConfRecord(confModel.isIfRecord() ? "1" : "0");
        confRest.setIsSuperConf(confModel.getIsSuperConf() ? "1" : "0");
        confRest.setLeavePrompt(confModel.getLeavePrompt());
        confRest.setRegSeatCount(String.valueOf(confModel.getMostNo()));
        confRest.setSpeakerCount(confModel.getMaxVoice());
        confRest.setsRTPUsageMode(confModel.getSrtpUsageMode());
        confRest.setSubPbxNo(confModel.getSubPbx());
        confRest.setSyncConfType(confModel.getConfMode());
        
        confRest.setConfId(confModel.getConfId());
        confRest.setAuthKey(confModel.getAuthKey());
        confRest.setStatus(null == confModel.getU19ConfStatus() ? "" : String.valueOf(confModel.getU19ConfStatus()));
        confRest.setType(null == confModel.getU19ConfType() ? "" : String.valueOf(confModel.getU19ConfType()));
        return confRest;
    }
    
    public EndConfResponse endConfModel2Rest(SDKResult<Conference> conf)
    {
        EndConfResponse response = new EndConfResponse();
        response.setResultCode(String.valueOf(conf.getErrCode()));
        response.setResultDesc(conf.getDescription());
        if (0 != conf.getErrCode())
        {
            return response;
        }
        response.setResultDesc(conf.getDescription());
        if (null != conf.getResult())
        {
            response.setConfRoom(conf.getResult().getAreaId());
            response.setMsgContentLen(conf.getResult().getMsgContentLen());
            response.setRecordNum(conf.getResult().getRecordNum());
            response.setSubPbxNo(conf.getResult().getSubPbx());
        }
        return response;
    }
    
    public List<Integer> endConfRest2Model(List<String> confRooms)
        throws SDKException
    {
        List<Integer> response = new ArrayList<Integer>();
        for (String confRoomStr : confRooms)
        {
            if (StringUtils.isInteger(confRoomStr))
            {
                response.add(Integer.parseInt(confRoomStr));
            }
            else
            {
                LOGGER.info("confRoom should be a number");
                SDKException sdkException = new SDKException(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
                sdkException.setSdkErrCode(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE);
                throw sdkException;
            }
        }
        
        return response;
    }
    
}
